package cn.sunzq.springdata.elasticsearch.demo.querydemo.controller;

import cn.sunzq.springdata.elasticsearch.demo.base.response.ResponseDTO;
import cn.sunzq.springdata.elasticsearch.demo.document.TempLincolnshireDocument;
import cn.sunzq.springdata.elasticsearch.demo.repositories.TempLincolnshireEsRepositories;
import cn.sunzq.springdata.elasticsearch.demo.repositories.TempLincolnshireRepositories;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @创建人 孙中泉
 * @创建时间 2021/7/26
 * @描述
 */
@RestController
@RequestMapping("/repository")
public class RepositoryController {

    @Autowired
    private TempLincolnshireRepositories tempLincolnshireRepositories;

    @Autowired
    private TempLincolnshireEsRepositories tempLincolnshireEsRepositories;

//    @Autowired
//    private ReactiveTempLincolnshireRepositories reactiveTempLincolnshireRepositories;

    @RequestMapping(value = "/getDocumentById/{id}", method = RequestMethod.POST, produces = "application/json;charset=utf-8")
    public ResponseDTO<TempLincolnshireDocument> getDocumentById(@PathVariable("id") String id){
        return ResponseDTO.defaultResponse(tempLincolnshireRepositories.findById(id));
    }

    @RequestMapping(value = "/findById/{id}", method = RequestMethod.POST, produces = "application/json;charset=utf-8")
    public ResponseDTO<TempLincolnshireDocument> findById(@PathVariable("id") String id){
        return ResponseDTO.defaultResponse(tempLincolnshireEsRepositories.findById(id));
    }

    @RequestMapping(value = "/getDocumentList/{name}/{year}/{month}", method = RequestMethod.POST, produces = "application/json;charset=utf-8")
    public ResponseDTO<List<TempLincolnshireDocument>> getDocumentList(@PathVariable("name") String name,
                                                                       @PathVariable("year") String year,
                                                                       @PathVariable("month") String month){
        return ResponseDTO.defaultResponse(tempLincolnshireRepositories.findByNameAndYearAndMonth(name, year, month));
    }

    @RequestMapping(value = {"/getDocumentListByPage/{name}/{pageNo}/{pageSize}","/getDocumentListByPage/{name}","/getDocumentListByPage/{name}/{pageNo}"},
            method = RequestMethod.POST, produces = "application/json;charset=utf-8")
    public ResponseDTO<List<TempLincolnshireDocument>> getDocumentListByPage(@PathVariable("name") String name,
                                                            @PathVariable(value = "pageNo", required = false) Integer pageNo,
                                                            @PathVariable(value = "pageSize", required = false) Integer pageSize){
        if(pageNo == null || pageNo <= 0)pageNo = 1;
        if(pageSize == null || pageSize <= 0)pageSize = 3;
        return ResponseDTO.defaultResponse(tempLincolnshireRepositories.findByName(name, PageRequest.of(pageNo - 1, pageSize)));
    }

}
